/*
 * @Author: TC
 * @Date: 2025-06-19 15:39:02
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-19 20:21:22
 */
import { UISkew, Vec3, v3 } from "cc";
import { EntityBase } from "../EntityBase";
import { app } from "db://assets/FrameWork/App";

/**
 * 实体速度
 */
export class EntitySpeed {
    private _entity: EntityBase = null;

    private speedVal = 200;
    private _angle: number = -1;
    private _entityPos: Vec3 = v3();

    private _speed: Vec3 = v3();

    public get entity(): EntityBase {
        return this._entity;
    }

    constructor(entity: EntityBase) {
        this._entity = entity;
    }

    public tick(dt: number): void {
        this.entity.node.getPosition(this._entityPos);
        // console.log(this._angle);
        if (this._angle < 0) {
            this._speed.set(0, 0);
        } else {
            this._speed.set(
                this.speedVal * app.math.cosAngle(this._angle) * dt,
                this.speedVal * app.math.sinAngle(this._angle) * dt
            );
        }
        // console.log("this._speed", this._speed);
        this.entity.node.setPosition(this._entityPos.add(this._speed));
        const skew = this.entity.act.ske.node.getComponent(UISkew);
        if (skew) {
            if (this._speed.x < 0) {
                skew.y = 8;
            } else if (this._speed.x > 0) {
                skew.y = -8;
            } else {
                skew.y = 0;
            }
        }
    }

    /**
     * 设置角度
     * @param angle
     */
    public setAngle(angle: number): void {
        this._angle = angle;
        // console.log("setAngle", angle);
    }
}
